package com.zhsj.OkeHomeConsumption.ui.activity.wallet;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * 名称：BaseFragment.java
 * 描述： Fragment 基类
 * 创建人：zhangpeisen
 * 创建时间：2016/11/30 10:59
 */
public class BaseFragment extends SupportFragment {

    /**
     * 依附的Activity
     */
    protected Activity mActivity = null;
    protected Context mContext = null;
    private static long lastClickTime;
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
        mActivity = activity;
    }

    Toast toastShort;

    /**
     * @Title: showShortToast @Description: TODO 弹出短时间的Toast @param @param
     * message @param @return @return Toast 返回类型 @throws
     */
    public Toast showShortToast(String message) {
        if (toastShort == null) {
            toastShort = Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT);
        } else {
            toastShort.setText(message);
            toastShort.setDuration(Toast.LENGTH_SHORT);
        }
        toastShort.setGravity(Gravity.CENTER, 0, 0);
        toastShort.show();
        return toastShort;
    }

    Toast toastLong;

    /**
     * @Title: showLongToast @Description: TODO 弹出长时间的Toast @param @param
     * message @param @return @return Toast 返回类型 @throws
     */
    public Toast showLongToast(String message) {
        if (toastLong == null) {
            toastLong = Toast.makeText(getActivity(), message, Toast.LENGTH_LONG);
        } else {
            toastLong.setText(message);
            toastLong.setDuration(Toast.LENGTH_LONG);
        }
        toastLong.setGravity(Gravity.CENTER, 0, 0);
        toastLong.show();
        return toastLong;
    }

    public void initImageLoader(Context context) {
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        config.writeDebugLogs(); // Remove for release app
        ImageLoader.getInstance().init(config.build());

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
    /**
     * 〈一句话功能简述〉
     * 防止快速点击
     *
     * @param
     * @return [返回类型说明]
     * @exception/throws
     * @see [类、类#方法、类#成员]
     */
    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }
    //查找控件
    @SuppressWarnings("unchecked")
    protected final <T extends View> T $(@NonNull View view, @IdRes int id) {
        return (T) (view.findViewById(id));
    }

}
